/**
 * Created by Dalyn on 2017/4/13.
 */
import React from "react";
import { Table, message } from 'antd';
import Relay from "react-relay";
import Reactkey from "../../../util/ReactKey";
import Packapply from './Packapply';
import Util from "../../../util/Util";
import AdditionalMutation from "./mutation/AdditionalMutation";
import './css/OrderDetailContractInfo.css';

export default class OrderdetailContractInfo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
          supplyContractsData: [],
          uid: sessionStorage.getItem('uid'),
          isShow: true,
          flag:false
        };
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.orderpartA = this.props.order.partA
    }

    //签订合同附件
    signedContractUrl(){
      let signedContractUrl = [];
      let signedContractUrlSum = [];
      if (this.props.order.signedContractUrl1) {
        signedContractUrl.push(Util.signedContractStr(this.props.order.signedContractUrl1))
        signedContractUrlSum.push(this.props.order.signedContractUrl1)
      }
      if (this.props.order.signedContractUrl2) {
        signedContractUrl.push(Util.signedContractStr(this.props.order.signedContractUrl2))
        signedContractUrlSum.push(this.props.order.signedContractUrl2)
      }
      if (this.props.order.signedContractUrl3) {
        signedContractUrl.push(Util.signedContractStr(this.props.order.signedContractUrl3))
        signedContractUrlSum.push(this.props.order.signedContractUrl3)
      }
      if (this.props.order.signedContractUrl4) {
        signedContractUrl.push(Util.signedContractStr(this.props.order.signedContractUrl4))
        signedContractUrlSum.push(this.props.order.signedContractUrl4)
      }
      if (this.props.order.signedContractUrl5) {
        signedContractUrl.push(Util.signedContractStr(this.props.order.signedContractUrl5))
        signedContractUrlSum.push(this.props.order.signedContractUrl5)
      }
      this.signedContractUrlx = signedContractUrl ? signedContractUrl : [];
      return this.signedContractUrlx.map((item2,index)=>{
          return(
              <Packapply oArr={item2} item2={item2} contractUrlSum={signedContractUrlSum[index]}/>
          )
      })
    }

    suppleUrl(url) {
      let suppleUrl = [];
      let suppleUrlSum = [];
      if (url.contractUrl) {
        suppleUrl.push(Util.signedContractStr(url.contractUrl))
        suppleUrlSum.push(url.contractUrl)
      } else if (url.contractUr2) {
        suppleUrl.push(Util.signedContractStr(url.contractUr2))
        suppleUrlSum.push(url.contractUr2)
      }
      else if (url.contractUr3) {
        suppleUrl.push(Util.signedContractStr(url.contractUr3))
        suppleUrlSum.push(url.contractUr3)
      }
      else if (url.contractUr4) {
        suppleUrl.push(Util.signedContractStr(url.contractUr4))
        suppleUrlSum.push(url.contractUr4)
      }
      else if (url.contractUr5) {
        suppleUrl.push(Util.signedContractStr(url.contractUr5))
        suppleUrlSum.push(url.contractUr5)
      }
      this.suppleUrlx = suppleUrl ? suppleUrl : [];
      return this.suppleUrlx.map((item2,index)=>{
          return(
              <Packapply oArr={item2} item2={item2} contractUrlSum={suppleUrlSum[index]}/>
          )
      })
    }

    contractSupplement() {
      this.refs.ordersupp.setState({
        visible: true,
        oid: this.props.order.oid,
        account: this.props.order.account,
      });
    }

    agree(supplyId,flag) {
      this.IsAreegn(0,supplyId);
    }

    disagree(supplyId) {
      this.IsAreegn(1,supplyId)
    }

    IsAreegn(intnum, supplyId){
        Relay.Store.commitUpdate(
            new AdditionalMutation({
                agree : intnum,
                orderId: this.state.oid,
                uid: this.state.uid,
                supplyId: supplyId,
                orderId: this.props.order.oid,
            }),
            {
                onFailure: () => {
                  message.error('请求失败')
                },
                onSuccess: (response) => {
                  if (intnum == '1') {
                    message.config({
                      top: 50,
                      duration: 3,
                    });
                    message.warning('您拒绝本次调整合同，如果需要调整，请联系甲方重新发起');
                    this.props.Orderdetail();
                  } else {
                    message.config({
                      top: 50,
                      duration: 3,
                    });
                    message.success('您已同意此合同');
                    this.props.Orderdetail();
                  }
                }
            }
        )
    }


    render() {

      const { supplyContractsData } = this.state;
      let status = ''
      if(supplyContractsData[0]){
        status = supplyContractsData[0].status||''
      }
        return (
          <div style={(this.enterpriseId != this.orderpartA) && (this.props.order.partB&&this.enterpriseId != this.props.order.partB) ? {display:'none'} : {display: 'block'}}>
            {

              //甲方 长度>= 1  && status == 1  ： 乙方  都显示  status == 0有同意和拒绝
              this.enterpriseId == this.orderpartA  ? (
                supplyContractsData.length >= 1 && status == 1 ? (
                  <div>
                    <p style={{fontSize: '18px',fontFamily: 'Microsoft Yahei',color: '#000',padding: '20px 0 20px'}}>补充合同</p>
                    {
                      supplyContractsData.length ? supplyContractsData.map((v,i)=>{
                        if(v.status == 1){
                          return (
                            <ul className='contractInfo_table' key={Reactkey.key}>
                              <li>
                                <span>时间</span>
                                <span>合同金额</span>
                                <span>调整后金额</span>
                                <span>调整理由</span>
                                <span>附件</span>
                              </li>
                              <li>
                                <span>{Util.dateFormat_hms(v.createDate)}</span>
                                <span>￥{(v.account/100).toFixed(2)}</span>
                                <span>￥{(v.supplyAccount/100).toFixed(2)}</span>
                                <span title={v.desp}>{v.desp}</span>
                                <span className="contractInfo_table_contract">
                                  {
                                    this.suppleUrl(v) == '' ? '无' : this.suppleUrl(v)
                                  }
                                </span>
                              </li>
                            </ul>
                          )
                        }
                      }) : null
                    }
                  </div>

                ) : null
              ) : (
                <div>
                  {
                    supplyContractsData.length && supplyContractsData.length != 0 ? (
                      <p style={{fontSize: '18px',fontFamily: 'Microsoft Yahei',color: '#000',padding: '20px 0 20px'}}>补充合同</p>
                    ) : null
                  } 
                  {
                    supplyContractsData.length > 0 ? supplyContractsData.map((v,i)=>{
                        return (
                          <ul className='contractInfo_table' key={Reactkey.key}>
                            <li>
                              <span>时间</span>
                              <span>合同金额</span>
                              <span>调整后金额</span>
                              <span>调整理由</span>
                              <span>附件</span>
                            </li>
                            <li>
                              <span>{Util.dateFormat_hms(v.createDate)}</span>
                              <span>￥{(v.account/100).toFixed(2)}</span>
                              <span>￥{(v.supplyAccount/100).toFixed(2)}</span>
                              <span title={v.desp}>{v.desp}</span>
                              <span className="contractInfo_table_contract">
                                {
                                  this.suppleUrl(v) == '' ? '无' : this.suppleUrl(v)
                                }
                              </span>
                            </li>
                            {
                               (this.enterpriseId != this.orderpartA && v.status == 0) ?
                               <li><button onClick={this.agree.bind(this, v.sid,this.state.flag)}>接受</button><button onClick={this.disagree.bind(this, v.sid)}>拒绝</button></li>
                                : null
                            }
                          </ul>
                        )
                    }) : null
                  }
                </div>
              )
            }

          </div>
        );
    }
}
